import java.util.Arrays;
import java.util.Comparator;

/**
 * @ClassName KP25_ObjectComparator
 * @Description: TODO
 * @Author: JCC
 * @CreateTime: 2023/10/25 15:11
 */

class Student {
    private Integer id;
    private String name;
    private Integer age;

    public Student(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

class IdComparator implements Comparator<Student> {

    /*
    * 返回负数，表示前面一个数排在前面
    * 返回正数，表示后面一个数排在前面
    * 返回0，表示两个数一样，无所谓
    */
    @Override
    public int compare(Student o1, Student o2) {
        // 升序排列
        // 前面原本小的继续排在前面 --> 升序排列
        //return o1.getId() - o2.getId();
        // 降序排列
        // 后面原本小的，前面大的依旧排在前面 --> 降序排列
        return o2.getId() - o1.getId();
    }
}

/**
 * 自定义对象比较器
 */
public class KP25_ObjectComparator {

    public static void printStudent(Student[] students) {
        for (Student s : students) {
            System.out.println("id：" + s.getId() + " name：" + s.getName() + " age：" + s.getAge());
        }
    }

    public static void main(String[] args) {
        Student s1 = new Student(6,"张三",33);
        Student s2 = new Student(2,"李四",25);
        Student s3 = new Student(4,"王五",17);
        Student s4 = new Student(3,"赵六",47);
        Student s5 = new Student(5,"陈七",37);
        Student s6 = new Student(1,"肖八",75);
        Student[] students = {s1,s2,s3,s4,s5,s6};
        printStudent(students);
        // 1、自定义比较器（降序排列）基本数据类型不能使用比较器；包装类数据类型才能使用比较器
        // Arrays.sort(students,new IdComparator());
        // 2、直接重写系统的Comparator方法
        /*Arrays.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getId() - o1.getId();
            }
        });*/
        // 3、lambda表达式：升序排列
        Arrays.sort(students,(o1,o2)->{return o1.getId()-o2.getId();});
        System.out.println("=========================");
        printStudent(students);
    }
}
